package com.baichang.taoshunda.utils;

import cn.jpush.api.common.ServiceHelper;
import cn.jpush.api.push.model.notification.PlatformNotification;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

import java.util.Map;

/**
 * @param
 * @author: wsl
 * @Date: 2019/1/18 16：56
 * @Description:
 */
public class IosNotification extends PlatformNotification {
    public static final String NOTIFICATION_IOS = "ios";

    private static final String DEFAULT_SOUND = "";
    private static final String DEFAULT_BADGE = "+1";

    private static final String BADGE = "badge";
    private static final String SOUND = "sound";
    private static final String CONTENT_AVAILABLE = "content-available";
    private static final String MUABLE_CONTENT = "mutable-content";
    private static final String CATEGORY = "category";

    private static final String ALERT_VALID_BADGE = "Badge number should be 0~99999, "
            + "and can be prefixed with + to add, - to minus";


    private final boolean soundDisabled;
    private final boolean badgeDisabled;
    private final String mutableContent;
    private final String sound;
    private final String badge;
    private final boolean contentAvailable;
    private final String category;

    private IosNotification(Object alert, String sound, String badge,
                            boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled,
                            String category,
                            Map<String, String> extras,
                            Map<String, Number> numberExtras,
                            Map<String, Boolean> booleanExtras,
                            Map<String, JsonObject> jsonExtras) {
        super(alert, extras, numberExtras, booleanExtras, jsonExtras);

        this.sound = sound;
        this.badge = badge;
        this.contentAvailable = contentAvailable;
        this.soundDisabled = soundDisabled;
        this.badgeDisabled = badgeDisabled;
        this.category = category;
        this.mutableContent = "1";
    }

    public static Builder newBuilder() {
        return new Builder();
    }


    @Override
    public String getPlatform() {
        return NOTIFICATION_IOS;
    }

    @Override
    public JsonElement toJSON() {
        JsonObject json = super.toJSON().getAsJsonObject();

        if (!badgeDisabled) {
            if (null != badge) {
                json.add(BADGE, new JsonPrimitive(this.badge));
            } else {
                json.add(BADGE, new JsonPrimitive(DEFAULT_BADGE));
            }
        }
        if (!soundDisabled) {
            if (null != sound) {
                json.add(SOUND, new JsonPrimitive(sound));
            } else {
                json.add(SOUND, new JsonPrimitive(DEFAULT_SOUND));
            }
        }
        if (contentAvailable) {
            json.add(CONTENT_AVAILABLE, new JsonPrimitive(1));
        }
        if (null != category) {
            json.add(CATEGORY, new JsonPrimitive(category));
        }
        if (null != mutableContent) {
            json.add(MUABLE_CONTENT, new JsonPrimitive(mutableContent));
        }

        return json;
    }


    public static class Builder extends PlatformNotification.Builder<IosNotification, Builder> {
        private String sound;
        private String badge;
        private boolean contentAvailable = false;
        private boolean soundDisabled = false;
        private boolean badgeDisabled = false;
        private String category;
        private String mutableContent;

        @Override
        protected Builder getThis() {
            return this;
        }

        public Builder setSound(String sound) {
            this.sound = sound;
            return this;
        }

        public Builder setMutableContent(String mutableContent) {
            this.mutableContent = mutableContent;
            return this;
        }

        public Builder disableSound() {
            this.soundDisabled = true;
            return this;
        }

        public Builder incrBadge(int badge) {
            if (!ServiceHelper.isValidIntBadge(Math.abs(badge))) {
                LOG.warn(ALERT_VALID_BADGE);
                return this;
            }

            if (badge >= 0) {
                this.badge = "+" + badge;
            } else {
                this.badge = "" + badge;
            }
            return this;
        }

        public Builder setBadge(int badge) {
            if (!ServiceHelper.isValidIntBadge(badge)) {
                LOG.warn(ALERT_VALID_BADGE);
                return this;
            }
            this.badge = "" + badge;
            return this;
        }

        /**
         * equals to: +1
         */
        public Builder autoBadge() {
            return incrBadge(1);
        }

        public Builder disableBadge() {
            this.badgeDisabled = true;
            return this;
        }

        public Builder setContentAvailable(boolean contentAvailable) {
            this.contentAvailable = contentAvailable;
            return this;
        }

        public Builder setCategory(String category) {
            this.category = category;
            return this;
        }

        @Override
        public Builder setAlert(Object alert) {
            this.alert = alert;
            return this;
        }

        @Override
        public IosNotification build() {
            return new IosNotification(alert, sound, badge, contentAvailable,
                    soundDisabled, badgeDisabled, category,
                    extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder);
        }
    }
}
